package com.ruoyi.web.controller.other;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.estate.domain.HouseEstateT;
import com.ruoyi.estate.service.IHouseEstateTService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 开发企业Controller
 * 
 * @author ruoyi
 * @date 2023-04-20
 */
@RestController
@RequestMapping("/other/estate")
public class HouseEstateTController extends BaseController
{
    @Autowired
    private IHouseEstateTService houseEstateTService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询开发企业列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseEstateT houseEstateT)
    {
        startPage();
        List<HouseEstateT> list = houseEstateTService.selectHouseEstateTList(houseEstateT);
        return getDataTable(list);
    }

    /**
     * 导出开发企业列表
     */
    @Log(title = "开发企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseEstateT houseEstateT)
    {
        List<HouseEstateT> list = houseEstateTService.selectHouseEstateTList(houseEstateT);
        ExcelUtil<HouseEstateT> util = new ExcelUtil<HouseEstateT>(HouseEstateT.class);
        util.exportExcel(response, list, "开发企业数据");
    }

    /**
     * 获取开发企业详细信息
     */
    @GetMapping(value = "/{estateId}")
    public AjaxResult getInfo(@PathVariable("estateId") Long estateId)
    {
        return AjaxResult.success(houseEstateTService.selectHouseEstateTByEstateId(estateId));
    }

    /**
     * 新增开发企业
     */
    @Log(title = "开发企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseEstateT houseEstateT)
    {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(houseEstateT.getUserName())))
        {
            return AjaxResult.error("新增用户'" + houseEstateT.getUserName() + "'失败，登录账号已存在");
        }
        HouseEstateT vo=new HouseEstateT();
        SysUser user=new SysUser();
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(houseEstateT.getPassword()));
        user.setUserName(houseEstateT.getEstateName());
        user.setNickName(houseEstateT.getEstateName());
        user.setRoleId(101L);
        userService.insertUser(user);
        houseEstateT.setCreater(getUsername());
        return toAjax(houseEstateTService.insertHouseEstateT(houseEstateT));
    }

    /**
     * 修改开发企业
     */
    @Log(title = "开发企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseEstateT houseEstateT)
    {
        return toAjax(houseEstateTService.updateHouseEstateT(houseEstateT));
    }

    /**
     * 删除开发企业
     */
    @Log(title = "开发企业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{estateIds}")
    public AjaxResult remove(@PathVariable Long[] estateIds)
    {
        return toAjax(houseEstateTService.deleteHouseEstateTByEstateIds(estateIds));
    }
}
